package com.guigu.qingcheng.controller;

import com.guigu.common_util.pojo.utils.JwtUtils;
import com.guigu.common_util.pojo.utils.Result;
import com.guigu.qingcheng.entity.TbUser;
import com.guigu.qingcheng.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * 转登录页面
     * @return
     */
    @GetMapping("loginUI")
    public String LoginUI() {
        return "login";
    }


    @PostMapping("/login")
    @ResponseBody
    public Result login(@RequestBody TbUser user){
        Result result = userService.loginUser(user);
        return result;
    }


    // 生成微信扫描的二维码
    @GetMapping("/api/ucenter/wx/login")
    @ResponseBody
    public String getWxCode(){
        return userService.getWxCode();
    }


    @GetMapping("getUserInfo")
    @ResponseBody
    public Result getUserInfo(HttpServletRequest request){
        // 调用jwt工具类的方法，根据request对象获取头信息，返回用户id
        String username = JwtUtils.getMemberIdByJwtToken2(request);
        Result result = userService.getUserInfo(username);
        return result;
    }
}
